package com.ciban.smarthome.module.ui;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.weiget.PinnedHeaderExpandableListView;
import com.ciban.smarthome.module.engine.SceneEngine;
import com.ciban.smarthome.module.entity.Area;
import com.ciban.smarthome.module.entity.Equipment;
import com.ciban.smarthome.module.ui.adapter.SceneEListAdapter;
import com.ciban.smarthome.module.ui.base.BaseActivity;

import java.util.ArrayList;

/**
 * Created by wdl on 2015/11/19.
 */
public class SceneDiyActivity extends BaseActivity implements View.OnClickListener {
    private SceneEngine engine;
    private ArrayList<Area> areaList;

    private PinnedHeaderExpandableListView plv_show;
    private ArrayList<ArrayList<Equipment>> equiList;
    private SceneEListAdapter adapter;
    private String scene_id; //预生成的场景id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scene_diy);

        initData();
        findViews();
        setListeners();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.title_right) {
            if (engine.saveScene()) {
               setResult(RESULT_OK);
                finish();
            }
        }

    }

    private void setListeners() {
        plv_show.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return false;
            }
        });
        plv_show.setOnHeaderUpdateListener(new PinnedHeaderExpandableListView.OnHeaderUpdateListener() {
            @Override
            public View getPinnedHeader() {
                View headerView = (ViewGroup) getLayoutInflater().inflate(R.layout.listitem_scene_group, null);
                headerView.setLayoutParams(new AbsListView.LayoutParams(
                        AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
                return headerView;
            }

            @Override
            public void updatePinnedHeader(View headerView, int firstVisibleGroupPos) {
                Area firstVisibleGroup = (Area) adapter.getGroup(firstVisibleGroupPos);
                TextView textView = (TextView) headerView.findViewById(R.id.scene_diy_group_title);
                textView.setText(firstVisibleGroup.getName());
            }
        });
        plv_show.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }

    private void findViews() {
        ((TextView) findViewById(R.id.title_desc)).setText("自定义场景");
        ((ImageView) findViewById(R.id.title_left)).setOnClickListener(this);
        ((TextView) findViewById(R.id.title_right)).setOnClickListener(this);
        plv_show = (PinnedHeaderExpandableListView) findViewById(R.id.scene_diy_list);

        adapter = new SceneEListAdapter(this, scene_id, areaList, equiList);
        adapter.setListener(new SceneEListAdapter.EquipChangedListener() {
            @Override
            public void equipStatusChanged(String sceneID, String equipID, int type, int status) {
                engine.addSceneEquip(equipID);
            }

            @Override
            public <T> T getEquipConfigDetails(String sceneID, String equipID, int type) {
                return engine.getEquipByType(sceneID, equipID, type);
            }
        });
        plv_show.setAdapter(adapter);


    }

    private void initData() {
        engine = new SceneEngine(this);
        areaList = engine.getAreaList();
        equiList = engine.getEquiList();
        scene_id = engine.getScene_id();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        engine.destory();
        engine = null;
    }


}
